package com.suncaper.wcss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.wcss.model.domain.BatchSetting;
import com.suncaper.wcss.mapper.BatchSettingMapper;
import com.suncaper.wcss.model.vo.BatchSettingVO;
import com.suncaper.wcss.service.BatchSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 批次设定 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2021-11-11
 */
@Service
public class BatchSettingServiceImpl extends ServiceImpl<BatchSettingMapper, BatchSetting> implements BatchSettingService {

    @Override
    public Page<BatchSettingVO> pageList(Page<BatchSetting> page, BatchSetting batch) {
        Page<BatchSettingVO> voPage =  baseMapper.pageList(page,batch);
        return voPage;
    }

    @Override
    @Transactional
    public void insert(BatchSetting batchSetting) {
        if(batchSetting.getActive()){
            //将已激活的设置为未激活
            baseMapper.updateActive();
        }

        //再添加
        baseMapper.insert(batchSetting);
    }

    @Override
    public Boolean updateBatch(Long id) {
        BatchSetting batchSetting = baseMapper.selectById(id);
        //保证当前时间可以禁用这个批次
        //判断当前时间是否在申请或选衣时间期间
        LocalDate now = LocalDate.now();
        //11  24:00
        if((now.isAfter(batchSetting.getApplicationStartDate())&&now.isBefore(batchSetting.getApplicationEndDate().plusDays(1)))
        ||(now.isAfter(batchSetting.getRegisterStartDate())&&now.isBefore(batchSetting.getRegisterEndDate().plusDays(1)))){
            //表示在此期间，批次不可改
            return Boolean.FALSE;
        }
        batchSetting.setActive(Boolean.FALSE);
        batchSetting.setGmtModified(LocalDateTime.now());
        baseMapper.updateById(batchSetting);
        return Boolean.TRUE;
    }

    @Override
    public BatchSetting curBatch() {
        QueryWrapper<BatchSetting> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BatchSetting::getActive,Boolean.TRUE);
        BatchSetting batchSetting = baseMapper.selectOne(wrapper);
        if(Objects.nonNull(batchSetting)){
            return batchSetting;
        }
        QueryWrapper<BatchSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(BatchSetting::getGmtCreated);
        BatchSetting batch = baseMapper.selectOne(wrapper);
        return batch;//在没有批次的情况会返回null
    }

    @Override
    public Boolean yanzheng(Long id) {
        BatchSetting batchSetting = baseMapper.selectById(id);
        if(Objects.isNull(batchSetting)){
            return Boolean.TRUE;
        }
        LocalDate now = LocalDate.now();
        if(now.isAfter(batchSetting.getRegisterStartDate())&&now.isBefore(batchSetting.getRegisterEndDate().plusDays(1))){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
